/**
 * Created by Marko on 3/30/14.
 */

$(document).ready(function() {


    // Init nicEdit-ors
    // nicEditors.allTextAreas();
    tinymce.init({
        selector:'textarea',
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});


    var formToSubmit = '#article_form';
    var formbutton = '#submit_form';


    // Add method to jQuery validator
    $.validator.addMethod('positive_non_zero',
        function(value) {

            return Number(value) > 0;
        });


    //FORM VALIDATION
    jQuery(formToSubmit).validate({
        rules: {
            page_id: 'required',
            title: 'required',
            content: 'required'

        },
        highlight: function(element) {
            $(element).addClass("error");
        },
        unhighlight: function(element) {
            $(element).removeClass("error");
        },
        submitHandler: function(form) {

            // triger tinymce save

            tinymce.triggerSave();

            //disable add button
            $(formbutton).attr("disabled", "disabled");

            var form = $(formToSubmit);
            var form_data = form.serialize();
            //alert(form_data);

            $.ajax({
                url: form.attr('action'),
                dataType: 'json',
                type: "POST",
                data: form_data,
                success: function(data, textStatus, xhr)
                {

                    if (data.action == true) {

                        /*
                         $("#freeow").freeow(localizedStrings['changesSaved'][locale], data.msg, {
                         classes: ["success"]
                         });
                         */
                        var n = noty(
                            {
                                text: localizedStrings['changesSaved'][locale] + '\n' + data.msg,
                                layout: 'bottomLeft',
                                type: 'success'

                            });

                        function action_delay() {
                            window.location.reload();
                        }

                        window.setTimeout(function() {
                            action_delay();
                        }, 2001);

                    } else {

                        $(formbutton).removeAttr("disabled");

                        var n = noty(
                            {
                                text: localizedStrings['error'][locale] + '\n' + data.msg,
                                layout: 'bottomLeft',
                                type: 'error'

                            });

                        $(formToSubmit).animate({
                            scrollTop: $(formToSubmit + ' .status_info').offset().top
                        }, 200);
                    }
                },
                error: function(xhr, textStatus, errorThrown) {
                    //enable add button
                    $(formbutton).removeAttr("disabled");
                    $(formToSubmit + ' .status_info').html(alert_html('Error ocured...\n' + xhr + '\n' + textStatus + errorThrown + '\n', 'error'));
                }
            });
        },
        errorPlacement: function(error, element) {
        }
    });

    $(formbutton).removeAttr("disabled");



})